import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains



class Base():
    # 初始化驱动
    def __init__(self,driver):
        self.driver=driver
    # 封装打开网址
    def open_url(self,url):
        self.driver.get(url)
    # 退出
    def quit(self):
        self.driver.quit()
    # 元素定位
    def find_element(self,loc):
        try:
            # 显示等待
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed)
            return self.driver.find_element(*loc)
        except Exception as e:
            str=time.strftime("%Y%m%d-%H%M%S")
            self.driver.save_screenshot(str+'.png')
            print(e)

    #输入
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    # 点击
    def click(self,loc):
        self.find_element(loc).click()

    # 文本
    def text(self,loc):
        return self.find_element(loc).text
    # 清除
    def clear(self,loc):
        clear=self.find_element(loc)
        clear.clear()

    # 鼠标事件：拖拽排序操作
    def action_move(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()

    def action_drop(self,loc):
        ActionChains(self.driver).click_and_hold(self.find_element(loc)).perform()
    # 下拉菜单的选择操作
    def select_value(self,loc,info):
        Select(self.driver).select_by_value(self.find_element(loc).send_keys(info))

    def select_index(self, loc, info):
        Select(self.driver).select_by_index(self.find_element(loc).send_keys(info))

    def select_text(self,loc,info):
        Select(self.driver).select_by_visible_text(self.find_element(loc).send_keys(info))

